package com.lagou.service;

import com.lagou.util.EmailUtil;
import com.lagou.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Service
@RefreshScope
public class EmailService {
    //发件人
    @Value("${email.from}")
    private String from;
    //邮件内容模板
    @Value("${email.template01}")
    private String template;
    //标题
    @Value("${email.title01}")
    private String title;
    //SMTP 服务器地址
    @Value("${email.emailSMTPHost}")
    private String emailSMTPHost;
    //授权码
    @Value("${email.emailPassword}")
    private String emailPassword;

    public boolean sendEmail(String to,String validCode) {
        System.out.println(from);
        System.out.println(template);
        System.out.println(title);
        System.out.println(emailSMTPHost);
        System.out.println(emailPassword);

//        String validCode = StringUtils.getValidCode(6);

        System.out.println("验证码为：" + validCode);
        String content = String.format(template, validCode);
        // 收件人电子邮箱
        boolean result = EmailUtil.sendEmail(emailSMTPHost, emailPassword, from, to, title, content);
        System.out.println("发送邮件结果：" + result);
        return result;
    }

}
